package org.op.service.fileio;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class FileRWServiceImpl implements FileRWService {

	public List<String> readFileText(String filepath) throws Exception {
		
		List<String> filelist = new ArrayList<String>();
		
        BufferedReader in = new BufferedReader(new FileReader(filepath));
        String str;
        while ((str = in.readLine()) != null) {
            filelist.add(str);
        }
        in.close();
		
		return filelist;
	}

	public void writeFileText(String filepath, List<String> contents) {
		
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new FileWriter(filepath));
			for (String line: contents) {
				out.write(line);
				out.newLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
            try {
                if (out != null) {
                	out.flush();
                	out.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
	}

}
